﻿using GalaSoft.MvvmLight;
using Jn698._45GWTool.Models.DataGrid;
using Jn698._45GWTool.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WebFirst.Entities;

namespace Jn698._45GWTool.ViewModels.Controls
{
    public class AddOrUpdateTransPlanViewModel:ViewModelBase
    {
        public static GWCmdMakeUp GWCmdMakeUp;
        private GWCmdMakeUpService gWCmdMakeUpService = new GWCmdMakeUpService();
        private int totalNum;

        public int TotalNum
        {
            get { return totalNum; }
            set { totalNum = value; RaisePropertyChanged(); GetDataInfo(value); }
        }

        private List<int> totalNumList;

        public List<int> TotalNumList
        {
            get { return totalNumList; }
            set { totalNumList = value; RaisePropertyChanged(); }
        }

        private string number;

        public string Number
        {
            get { return number; }
            set { number = value; RaisePropertyChanged(); }
        }

        private string deep;

        public string Deep
        {
            get { return deep; }
            set { deep = value; RaisePropertyChanged(); }
        }

        private string groupName;

        public string GroupName
        {
            get { return groupName; }
            set { groupName = value; RaisePropertyChanged(); }
        }

        private int selectedIndex;

        public int SelectedIndex
        {
            get { return selectedIndex; }
            set { selectedIndex = value; RaisePropertyChanged(); GroupName = $"第{value + 1}个配置单元"; UpDataInfo(); }
        }

        private ObservableCollection<GWCmdMakeUp> gWCmdMakeUpList;

        public ObservableCollection<GWCmdMakeUp> GWCmdMakeUpList
        {
            get { return gWCmdMakeUpList; }
            set { gWCmdMakeUpList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<SingleData> contentList;

        public ObservableCollection<SingleData> ContentList
        {
            get { return contentList; }
            set { contentList = value; RaisePropertyChanged(); }
        }

        public AddOrUpdateTransPlanViewModel()
        {
            ContentList = new ObservableCollection<SingleData>();
            GWCmdMakeUpList = new ObservableCollection<GWCmdMakeUp>();
            TotalNumList = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
        }

        public void GetDataInfo(int total)
        {
            GWCmdMakeUpList = new ObservableCollection<GWCmdMakeUp>(gWCmdMakeUpService.SelectByOAD("60187F00", total));
            if (GWCmdMakeUpList.Count <= 0)
            {
                return;
            }

            SelectedIndex = 0;
            GWCmdMakeUp = null;

        }
        public void UpDataInfo()
        {
            if (SelectedIndex < 0)
            {
                SelectedIndex = 0;
            }
            if (GWCmdMakeUpList.Count <= 0)
            {
                return;
            }
            //判断之前是否点击过，如果点击过则保存之前的数据
            if (GWCmdMakeUp != null)
            {
                SaveDataInfo();
            }

            GWCmdMakeUp = GWCmdMakeUpList[SelectedIndex];
            if (GWCmdMakeUp == null)
            {
                return;
            }
            try
            {
                ContentList.Clear();
                var datas = GWCmdMakeUp.DataValue.Split(';');
                if (datas.Length < 2)
                {
                    return;
                }
                Number = datas[0];
                for (int i = 0; i < datas.Length-2; i++)
                {
                    ContentList.Add(new SingleData{ Name= datas[1 + i] });
                }
                Deep = datas[datas.Length-1];
                
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }


        }

        public void SaveDataInfo()
        {
            if (GWCmdMakeUp == null)
            {
                return;
            }
            StringBuilder stringBuilder = new StringBuilder();



            //GWCmdMakeUp.DataValue = stringBuilder.ToString();
            //gWCmdMakeUpService.Update(GWCmdMakeUp);
        }
    }
}
